package com.atguigu.genenrator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author atguigu
 * @created 2022-10-28-12:53.
 */
public class CodeGenerator {

    public static void main(String[] args) {


        String url = "jdbc:mysql://192.168.31.122:3306/louke?serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true";
        String driverName = "com.mysql.cj.jdbc.Driver";
        String username = "root";
        String password = "interviewhub@mysql";

        DbType dbType = DbType.MYSQL;
        IdType idType = IdType.AUTO;
        String tablePrefix = "biz_";

        // 配置是否生成service，controller
        boolean genService = true;
        boolean genController = true;
        //Boolean extendBaseEntity = false;

        // 多个表名，逗号分隔
        String tableNames = "biz_purchase_management";

        // 项目路径
        String projectPath = System.getProperty("user.dir");
        String modulePath = projectPath + File.separator + "service-system";
        String modelPath = projectPath + File.separator + "model";
        String moduleName = "system";

        String author = "";
        String packagePath = "com.atguigu";

        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 2、全局配置
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(modulePath + "/src/main/java");
        gc.setAuthor(author);
        gc.setOpen(false);
        gc.setSwagger2(true);
        gc.setIdType(idType);//设置主键策略
        gc.setBaseResultMap(true);//生成基本ResultMap
        gc.setBaseColumnList(true);//生成基本ColumnList
        gc.setServiceName("%sService");//去掉服务默认前缀 ，Service接口的首字母I
        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driverName);
        dsc.setUsername(username);
        dsc.setPassword(password);
        dsc.setDbType(dbType);
        dsc.setTypeConvert(new MySqlTypeConvert() {
            @Override
            public IColumnType processTypeConvert(GlobalConfig globalConfig, TableField tableField) {
                return super.processTypeConvert(globalConfig, tableField);
            }
        });
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(packagePath);
        pc.setModuleName(moduleName); //模块名
        pc.setEntity("model.entity");//分模块后对entity包的路径修改
        mpg.setPackageInfo(pc);

        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 velocity
        String templatePath = "/templates/mapper.xml.vm";
        String entityPath = "/templates/entity.java.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return modulePath + "/src/main/resources/mapper/" + moduleName + File.separator + tableInfo.getEntityName()
                        + "Mapper" + StringPool.DOT_XML;
            }
        });
        // 自定义 entity.java 生成
        focList.add(new FileOutConfig(entityPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return modelPath + "/src/main/java/" + packagePath.replace(".", "/") + File.separator + moduleName + File.separator +
                        "model" + File.separator + "entity" + File.separator + tableInfo.getEntityName() + StringPool.DOT_JAVA;
            }
        });


        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 判断是否生成controller
        if (!genController) {
            templateConfig.setController(null);
        }
        // 判断是否生成service
        if (!genService) {
            templateConfig.setService(null);
            templateConfig.setServiceImpl(null);
        }
        templateConfig.setXml(null);
        templateConfig.setEntity(null);
        mpg.setTemplate(templateConfig);


        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();

        strategy.setInclude(tableNames.split(","));

        strategy.setTablePrefix(tablePrefix.split(","));
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        strategy.setLogicDeleteFieldName("is_deleted");//设置逻辑删除
        //strategy.setTablePrefix("m_");
        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }
}
